package com.soft.mainsite.module.web.cms;

import com.soft.mainsite.common.template.TemplateFile;
import com.soft.mainsite.common.exception.SystemException;
import com.soft.mainsite.common.template.TemplateFileService;
import com.soft.mainsite.common.utils.JsonUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Description:
 *
 * @author Jin
 * @create 2017-08-15
 **/
@Controller
@RequestMapping("/system/cms/template")
public class TemplateController {

    @Autowired
    private TemplateFileService templateFileService;


    @RequiresPermissions("template:admin")
    @RequestMapping
    public String index(Model model) {
        List<TemplateFile> list = templateFileService.findAll();
        model.addAttribute("templateFiles", list);
        return "cms/template";
    }


    @RequiresPermissions("template:edit")
    @RequestMapping("/input")
    public String input(TemplateFile templateFile, Model model) {
        if (templateFile.getFilePath() == null) {
            throw new SystemException("模板路径不能为空！");
        }
        model.addAttribute("templateFile", templateFileService.findByPath(templateFile.getFilePath()));
        return "cms/template_input";
    }


    @RequiresPermissions("template:save")
    @RequestMapping("/save")
    @ResponseBody
    public String save(TemplateFile templateFile){
        templateFileService.writeTemplateFileContent(templateFile);
        return JsonUtil.toSuccess("模板修改成功","template-tab",false);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public String insert(TemplateFile templateFile) {
        templateFileService.saveTemplateFileNameAndPath(templateFile);
        return JsonUtil.toSuccess("模板修改成功", "template-tab", false);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(TemplateFile templateFile) {
        if (templateFile.getFilePath() == null){
            throw new SystemException("模板路径为空，请检查后重新再试！");
        }
        Boolean flag = templateFileService.deleteByPath(templateFile.getFilePath());
        if (flag){
            return JsonUtil.toSuccess("模板删除成功！", "template-tab", false);
        }else {
            return JsonUtil.toError("模板删除失败！");
        }
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestParam("files") MultipartFile[] files, @RequestParam("parentPath") String parentPath) {
        if (files.length < 1){
            return JsonUtil.toError("文件为空，请选择后重新上传！");
        }
        templateFileService.add(files, parentPath);
        return JsonUtil.toSuccess("模板添加成功！", "template-tab", false);
    }

    @RequestMapping("/template_file_input")
    public String templateFileInput(TemplateFile templateFile, Model model) {
        if (templateFile.getFilePath() == null) {
            throw new SystemException("模板路径不能为空！");
        }
        model.addAttribute("templateFile", templateFileService.findByPath(templateFile.getFilePath()));
        return "cms/template_file_input";
    }


}
